const Koa = require('koa')
const app = new Koa()
// const bodyparser = require('koa-bodyparser')
const koaBody = require('koa-body')
const error = require('koa-json-error')
const parameter = require('koa-parameter')
const koaStatic = require('koa-static')
const path = require('path')

const userRouter = require('./router/user')
const homeRouter = require('./router/home')
app.use(koaStatic(path.join(__dirname, 'public')));

// 错误处理
app.use(error({
  postFormat: (e, { stack, ...rest }) => process.env.NODE_ENV === 'production' ? rest : { stack, ...rest }
}));

app.use(koaBody({
  multipart: true,
  formidable: {
    uploadDir: path.join(__dirname, '/public/uploads'),
    keepExtensions: true,
  },
}));
// 参数校验
app.use(parameter(app));

// app.use(bodyparser()) // 处理参数 只能处理json form格式
app.use(userRouter.routes(), userRouter.allowedMethods())
app.use(homeRouter.routes(), homeRouter.allowedMethods())

app.listen(5555, () => console.log(5555))